package org.ivwsqii.singleton;

/**
 * 饿汉
 * 直接实例化，有可能浪费内存(未使用时)
 * @author Administrator
 *
 */
public class Singleton {

	private static Singleton INSTANCE = new Singleton();

	private Singleton() {}

	public static Singleton getInstance(){
		return INSTANCE;
	}
	public static void main(String[] args) {
		SingletonEnum instace = SingletonEnum.INSTACE;
	}
}
/**
 * 饿汉式变形 同上
 * @author Administrator
 *
 */
class SingletonStatic{
	private static SingletonStatic instance;
	static {
		instance = new SingletonStatic();
	}

	private SingletonStatic(){}

	public static SingletonStatic getInstance(){
		return instance;
	}
}
/**
 * 内部类方式
 * @author Administrator
 *
 */
class SingletonInner{
	private SingletonInner(){}
	private static class Instance{
		private static SingletonInner INSTANCE = new SingletonInner();
	}
	public static SingletonInner getInstance(){
		return Instance.INSTANCE;
	}
}
/**
 * 双重检验方式
 * @author Administrator
 *
 */
class SingleDoubleCheck{
	private static SingleDoubleCheck instance;

	private SingleDoubleCheck(){}

	public static SingleDoubleCheck getInstance(){
		if(instance == null){
			synchronized(SingleDoubleCheck.class){
				if(instance == null){
					instance = new SingleDoubleCheck();
				}
			}
		}
		return instance;
	}
}
/**
 * 枚举方式
 * 相当于
 * public SingletonEnun extends Enum{
 * 	private static final SingletonEnum INSTANCE;
 * }
 * 可以直接调用SingletonEnum instance = SingletonEnum.INSTANCE;
 * @author Administrator
 *
 */
enum SingletonEnum{
	INSTACE;

}

